package com.chinasofti.dao;

import com.chinasofti.util.ConnectionFactory;
import com.chinasofti.vo.Meetingroom;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MeetingroomDao {
    private Connection conn;
    public List<Meetingroom> selectAllMeetings() {
        List<Meetingroom> list = new ArrayList<>();
        Meetingroom meetingroom = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "select * from meetingroom ";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);

            rs = pst.executeQuery();
            while (rs.next()){
                meetingroom = new Meetingroom();
                meetingroom.setRoomid(rs.getInt("roomid"));
                meetingroom.setRoomnum(rs.getInt("roomnum"));
                meetingroom.setRoomname(rs.getString("roomname"));
                meetingroom.setCapacity(rs.getInt("capacity"));
                meetingroom.setStatus(rs.getString("status"));
                meetingroom.setDescription(rs.getString("description"));
                list.add(meetingroom);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return list;
    }
    public Meetingroom selectroom(int roomid) {
        Meetingroom meetingroom = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "select * from meetingroom where roomid=?";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);

            pst.setInt(1,roomid);
            rs = pst.executeQuery();
            while (rs.next()){
                meetingroom = new Meetingroom();
                meetingroom.setRoomid(rs.getInt("roomid"));
                meetingroom.setRoomnum(rs.getInt("roomnum"));
                meetingroom.setRoomname(rs.getString("roomname"));
                meetingroom.setCapacity(rs.getInt("capacity"));
                meetingroom.setStatus(rs.getString("status"));
                meetingroom.setDescription(rs.getString("description"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return meetingroom;
    }

    public void updateMeetingroom(Meetingroom room) {
        conn = ConnectionFactory.getConnection();
        PreparedStatement pst = null;
        String sql = "update meetingroom set roomnum=?,roomname=?,capacity=?,status=?" +
                ",description=? where roomid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1,room.getRoomnum());
            pst.setString(2,room.getRoomname());
            pst.setInt(3,room.getCapacity());
            pst.setString(4,room.getStatus());
            pst.setString(5,room.getDescription());
            pst.setInt(6,room.getRoomid());
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
    }
}
